package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 *
 * @author ttao
 * @since 2023/4/23 9:28
 */

@Api(tags = "分类接口")
@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类
     *
     * @param categoryDTO
     * @return
     */
    @ApiOperation("添加菜品")
    @PostMapping
    public Result<String> save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        return categoryService.save(categoryDTO) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    /**
     * 分类分页条件查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @ApiOperation("分类分页条件查询")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.selectByPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @ApiOperation("删除分类")
    @DeleteMapping
    public Result<String> deleteById(Long id) {
        log.info("删除分类：{}", id);
        return categoryService.deleteById(id) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    @ApiOperation("更新分类")
    @PutMapping
    public Result<String> updateById(@RequestBody CategoryDTO categoryDTO) {
        log.info("更新分类：{}", categoryDTO);
        return categoryService.updateById(categoryDTO) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    @ApiOperation("启用、禁用分类")
    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("更新启用/禁用状态:{}-{}", status, id);
        return categoryService.startOrStop(status, id) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type) {
        List<Category> categoryList = categoryService.list(type);
        return Result.success(categoryList);
    }
}
